package com.goldenautumn.prefabricate.wxapi

import android.annotation.SuppressLint
import android.content.Intent
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.activity.mine.order.OrderListActivity
import com.goldenautumn.prefabricate.databinding.ActivityWxpayEntryBinding
import com.goldenautumn.prefabricate.utils.Constants.BUSINESS_PAGE_DATA
import com.gyf.immersionbar.ImmersionBar
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import kotlinx.coroutines.isActive
import qncx.external.common.base.BaseActivity

class WXPayEntryActivity : BaseActivity<ActivityWxpayEntryBinding>(), IWXAPIEventHandler {

    var api: IWXAPI? = null

   private val payResult:String?
        get() = intent.getStringExtra(BUSINESS_PAGE_DATA)

    override fun getLayoutId(): Int = R.layout.activity_wxpay_entry

    override suspend fun main() {
        ImmersionBar.setTitleBar(this, mBinding.toolbar)
        initToolbarNav(mBinding.toolbar)
        api = WXAPIFactory.createWXAPI(this, getString(R.string.wechatId), false)
        api?.handleIntent(intent, this)

        mBinding.backBtn.setOnClickListener {
            OrderListActivity.start(this,4)
            finish()
        }

        setPayResult()

        while (isActive) {
            events.receive()
        }

    }

    override fun onReq(p0: BaseReq?) {

    }

    override fun onResp(p0: BaseResp) {
        LogUtils.d("微信支付=${p0.errCode},${p0.errStr}")
    }

    @SuppressLint("MissingSuperCall")
    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        setIntent(intent)
        api?.handleIntent(intent, this)
    }

    fun setPayResult() {
        if(payResult != null) {
            var price = SPUtils.getInstance().getString("PRICE")
            mBinding.price.text = String.format(getString(R.string.currency), "$price")
            SPUtils.getInstance().remove("PRICE")
            if(payResult == "9000") {
                mBinding.title.setText(R.string.payment_successful)
                mBinding.payStatus.setImageResource(R.mipmap.ic_payment_success)
                mBinding.title.setTextColor(ContextCompat.getColor(this,R.color.colorPrimary))
            } else {
                mBinding.msg.text = if(payResult.isNullOrEmpty()) getString(R.string.payment_fail_msg) else payResult
                mBinding.title.setText(R.string.payment_fail)
                mBinding.title.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(this,
                    R.drawable.switch_track
                ),null, null, null)
                mBinding.title.setTextColor(ContextCompat.getColor(this,R.color.color_DE0000))
            }
        }
    }


}